import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/bar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/dialogs/message_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/cooperation_controller/cooperation_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/utils/invitation_copy_util.dart';

class CooperationPageActivity extends StatelessWidget {

  final CommonPageController commonPageController = Get.find();
  final CooperationPageController cooperationPageController;

  CooperationPageActivity({
    required this.cooperationPageController
  });


  @override
  Widget build(BuildContext context) {
    return Obx(() => _getCooperationStatus());
  }

  /// 根据活动状态、以及用户是否加入团队给出布局
  Widget _getCooperationStatus() {
    if (commonPageController.versionData.value.isSign == 1 && commonPageController.userData.value.teamId != 0) {
      return _activityOpenAndTeam();
    } else if (commonPageController.versionData.value.isSign == 0 && commonPageController.userData.value.teamId != 0) {
      return _activityNotOpenAndTeam();
    }else {
      return _activityOpenAndNotTeam();
    }
  }

  /// 活动未开启，已填写邀请码
  Widget _activityNotOpenAndTeam() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.h),
      margin: EdgeInsets.only(top: 12.h),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.h),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5.w),
          color: bgWhiteColor
        ),
        child: Text(
          '我们即将推出新一轮的邀请活动，\n记得关注我们的通知哦~',
          textAlign: TextAlign.center,
          style: TextStyle(
            color: textBlackColor,
            fontSize: 14.sp
          ),
        ),
      ),
    );
  }

  /// 活动已开启，未填写邀请码，活动未开启，未填写邀请码
  Widget _activityOpenAndNotTeam() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.h),
      margin: EdgeInsets.only(top: 12.h),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
          decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5.w),
          color: bgWhiteColor
        ),
        child: Column(
          children: [
            InkWellUtil.shakeProofInkWell(Image.asset(
              'assets/icons/icon_invitation.png', 
              width: 48.w,
              height: 48.w,
            ), () {
              cooperationPageController.invitationsDialog();
            }),
            SizedBox(height: 12.h),
            Text(
              '输入推荐人的邀请码加入我们吧~\n如果没有邀请码，可以联系客服获取哦~',
              textAlign: TextAlign.center,
              style: TextStyle(
                color: textBlackColor,
                fontSize: 14.sp
              ),
            ),
            SizedBox(height: 12.h),
            Container(
              width: 253.w,
              child: ButtonUtil(
                text: '联系客服', 
                fontSize: 14.sp, 
                borderRadius: 36.h, 
                height: 36.h, 
                onTop: () {
                  commonPageController.openWeChatCustomerServiceChat();
                }
              ),
            )
          ],
        ),
      ),
    );
  }

  /// 活动已开启，已填写邀请码
  Widget _activityOpenAndTeam() {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 12.h),
          margin: EdgeInsets.only(top: 12.h),
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5.w),
              color: bgWhiteColor
            ),
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 12.w),
                  child: Column(
                    children: [
                      BarUtil(title: '我的邀请', padding: 0),
                      InkWellUtil.shakeProofInkWell(Row(
                        children: [
                          Image.asset(
                            'assets/icons/icon_invitation_statistics.png',
                            width: 42.w,
                            height: 42.w,
                          ),
                          SizedBox(width: 6.w),
                          Text(
                            '我已经邀请到的新用户',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp
                            ),
                          ),
                          Expanded(child: Text(
                            '${commonPageController.userData.value.newUsers}人',
                            textAlign: TextAlign.end,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp
                            ),
                          )),
                          Image.asset(
                            'assets/icons/icon_arrow_right.png',
                            width: 14.w,
                            height: 14.w,
                          )
                        ],
                      ), () {
                        Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.MY_INVITATION_PAGE}');
                      }),
                      SizedBox(height: 12.h),
                      InkWellUtil.shakeProofInkWell(Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Image.asset(
                            'assets/icons/icon_actual_price.png',
                            width: 42.w,
                            height: 42.w,
                          ),
                          SizedBox(width: 6.w),
                          Text(
                            '已实际获得补贴',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp
                            ),
                          ),
                          SizedBox(width: 4.w),
                          InkWellUtil.shakeProofInkWell(Image.asset(
                            'assets/icons/icon_doubt.png',
                            width: 16.w,
                            height: 16.w,
                          ), () {
                            Get.dialog(MessageDialog(message: '邀请的新用户在一个自然日内完成四单商品抢购，推荐人一次性获得150元补贴金，每个新用户只奖励一次！'));
                          }),
                          Expanded(child: Text(
                            '¥${commonPageController.userData.value.actualSubsidy}',
                            textAlign: TextAlign.end,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp
                            ),
                          )),
                          Image.asset(
                            'assets/icons/icon_arrow_right.png',
                            width: 14.w,
                            height: 14.w,
                          )
                        ],
                      ), () {
                        // Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.MY_INVITATION_PAGE}');
                        Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.SUBSIDY_PAGE}');
                      })
                    ],
                  ),
                ),
                SizedBox(height: 12.h),
                // commonPageController.userData.value.newUsers! <= 0 ? Container() : Container( 
                //   padding: EdgeInsets.all(12.w),
                //   decoration: BoxDecoration(
                //     border: Border(
                //       top: BorderSide(
                //         width: .5,
                //         color: lineColor
                //       )
                //     )
                //   ),
                //   child: Column(
                //     children: [
                //       _statisticsBar(title: '预计获得¥${commonPageController.userData.value.expectedSubsidy}'),
                //       SizedBox(height: 12.h),
                //       InkWellUtil.shakeProofInkWell(Stack(
                //         children: [
                //           // _statisticsBar(title: '实际获得¥${commonPageController.userData.value.actualSubsidy}', showRight: true, bgColor: actualAcquisitionBg)
                //           _statisticsBar(title: '实际获得¥${commonPageController.userData.value.actualSubsidy}', showRight: true, bgColor: bgWhiteColor),
                //           Positioned(child: Container(
                //             height: 24.h,
                //             width: _getProgressBarWidth('${commonPageController.userData.value.expectedSubsidy}', '${commonPageController.userData.value.actualSubsidy}').w,
                //             color: actualAcquisitionBg,
                //           ))
                //         ],
                //       ), () {
                //         Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.SUBSIDY_PAGE}');
                //       })
                //     ],
                //   ),
                // )
              ],
            ),
          ),
        ),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 12.w),
          margin: EdgeInsets.only(top: 12.h),
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.h),
            decoration: BoxDecoration(
              color: bgWhiteColor,
              borderRadius: BorderRadius.circular(5.w)
            ),
            alignment: Alignment.center,
            child: Text.rich(
              TextSpan(
                children: InvitationCopyUtil.getCopywriting(
                  '${commonPageController.userData.value.expectedSubsidy}', 
                  '${commonPageController.userData.value.actualSubsidy}',
                  commonPageController.userData.value.newUsers!).map((item) {
                  return TextSpan(
                    text: item['label'],
                    style: TextStyle(
                      color: item['value'] ? mainTextColor : textBlackColor,
                      fontSize: 14.sp
                    )
                  );
                }).toList()
              ),
              textAlign: TextAlign.center,
            )
          ),
        )
      ],
    );
  }

  // Widget _statisticsBar({ required String title, bool showRight = false, Color? bgColor }) {
  //   return Row(
  //     children: [
  //       Container(
  //         height: 24.h,
  //         width: 180.w,
  //         color: bgColor ?? bgRecoveryStatus,
  //       ),
  //       SizedBox(width: 20.w),
  //       Expanded(child: Text(
  //         title,
  //         overflow: TextOverflow.ellipsis,
  //         maxLines: 1,
  //         style: TextStyle(
  //           color: textBlackColor,
  //           fontSize: 14.sp
  //         ),
  //       )),
  //       showRight ? Image.asset(
  //         'assets/icons/icon_arrow_right.png',
  //         width: 14.w,
  //         height: 14.w,
  //       ) : Container()
  //     ],
  //   );
  // }

  // /// 进度条宽度
  // double _getProgressBarWidth(String totalPrice, String thisPrice) {
  //   double _totalWidth = 180;
  //   double totalPriceD = double.parse(totalPrice);
  //   double thisPriceD = double.parse(thisPrice);
  //   double _width = 0;
  //   if (totalPriceD <= 0 || thisPriceD <= 0) {
  //     _width = NumUtil.multiply(NumUtil.divide(_totalWidth, 100), 1);
  //     return _width;
  //   }
  //   if (totalPriceD < thisPriceD) {
  //     return _totalWidth;
  //   }
  //   double tempWidth = NumUtil.divide(_totalWidth, totalPriceD);
  //   _width = NumUtil.multiply(tempWidth, thisPriceD);
  //   return _width;
  // }
}